package com.approval.approvalapp.index.cygn.yjcn;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.YjCnBean;
import com.approval.approvalapp.center.acitivty.JyHfXqDbActivity;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class YjcnListActivity extends BaseActivity {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.rv)
    RecyclerView rv;
    private List<YjCnBean> yjCnBeans;
    Context context;
    @Bind(R.id.empty_ll)
    LinearLayout em;

    @Override
    public int getLayoutId() {
        return R.layout.activity_yjcn;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        NetRequsetUtil.getInstance().netRequestGet("v1/user/get_my_idea_list", new NetDownResponse() {
            @Override
            public void success(String str) {
                yjCnBeans = JSON.parseArray(str, YjCnBean.class);
                rv.setLayoutManager(new LinearLayoutManager(YjcnListActivity.this));
                RvAdapter rvAdapter = new RvAdapter();
                rvAdapter.setYjCnBeans(yjCnBeans);
                rv.setAdapter(rvAdapter);

                if (yjCnBeans == null || yjCnBeans.size() == 0){
                    em.setVisibility(View.VISIBLE);
                }else {
                    em.setVisibility(View.GONE);
                }
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {

            }
        });
    }


    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }

    class RvAdapter extends RecyclerView.Adapter<RvAdapter.Holder> {
        private List<YjCnBean> yjCnBeans;

        public void setYjCnBeans(List<YjCnBean> yjCnBeans) {
            this.yjCnBeans = yjCnBeans;
            notifyDataSetChanged();
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            context = parent.getContext();
            View view = LayoutInflater.from(YjcnListActivity.this).inflate(R.layout.yjcn_item, parent, false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.tv1.setText(yjCnBeans.get(position).getWJTITLE());
            holder.tv2.setText(yjCnBeans.get(position).getWJTIME());
            if (yjCnBeans.get(position).getWJIFCY() == 1) {
                holder.tv3.setText("采纳");
                holder.tv3.setTextColor(Color.parseColor("#FF3A79F1"));
            } else if (yjCnBeans.get(position).getWJIFCY() == 2){
                holder.tv3.setText("不采纳");
                holder.tv3.setTextColor(Color.parseColor("#FFDE463A"));
            }else {
                holder.tv3.setText("待处理");
                holder.tv3.setTextColor(Color.parseColor("#FFF8AF35"));
            }

            holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("id", yjCnBeans.get(position).getWJID());
                    intent.putExtra("state", "已办");
                    intent.putExtra("title" , yjCnBeans.get(position).getWJTITLE());
                    intent.setClass(context, JyHfXqDbActivity.class);
                    context.startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            if (yjCnBeans == null) {
                return 0;
            } else {
                return yjCnBeans.size();
            }
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv1, tv2, tv3;
            ConstraintLayout constraintLayout;

            public Holder(View itemView) {
                super(itemView);
                tv1 = itemView.findViewById(R.id.tv1);
                tv2 = itemView.findViewById(R.id.tv2);
                tv3 = itemView.findViewById(R.id.tv3);
                constraintLayout = itemView.findViewById(R.id.root);
            }
        }
    }
}
